package com.citywithincity.api;

import java.io.IOException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.citywithincity.jobqueue.TaskQueue;

import com.citywithincity.db.ConnectionDescription;
import com.citywithincity.db.DbConfigUtil;
import com.citywithincity.db.DruidDataSourceProvider;
import com.citywithincity.db.interfaces.IDataSourceProvider;
import com.citywithincity.interfaces.IService;
import com.citywithincity.utils.ConfigReader;
import com.citywithincity.utils.HttpUtil;
import com.damai.action.Actions;
import com.damai.action.ApiInterfaces;
import com.damai.action.IActionFactory;
import com.damai.action.factories.ApiJsonActionFactory;
import com.damai.action.impl.ServerNotifyAction;
import com.damai.action.impl.TokenAction;
import com.damai.aop.impl.AopAsyncJobFactory;
import com.damai.controller.GenerateService;
import com.damai.controller.PhpVersionController;
import com.damai.controller.StructsApi;
import com.damai.controller.StructsWebController;
import com.damai.jobcenter.JobCenterApi;
import com.damai.jobcenter.QuartzPlugin;
import com.damai.pay.PayServerNotifyAction;
import com.damai.plugins.IPlugin;
import com.damai.plugins.impl.ApacheHttpPlugin;
import com.damai.plugins.impl.DbPlugin;
import com.damai.plugins.impl.MemcachePlugin;
import com.damai.proxy.ProxyAction;
import com.damai.proxy.ProxyApi;
import com.damai.proxy.ProxyParamAction;

/**
 * 主要的配置
 * 
 * @author renxueliang
 *
 */
public class ApiConfig extends AbsModule {

	private static Log logger;
	
	private ApacheHttpPlugin httpClientPlugin;
	private static boolean debug = true;
	
	
	public ApiConfig(){
		config = this;
	}
	
	
	private static ApiConfig config;
	
	public static ApiConfig me(){
		return config;
	}
	
	public static boolean debug() {
		return debug;
	}
	
	/**
	 * 是否记录接收参数和返回数据
	 */
	public static boolean out = false;
	/**
	 * 是否记录接收参数
	 */
	public static boolean in = false;
	
	/**
	 * 是否记录数据库sql
	 */
	public static boolean debugDatabase = true;
	

	public static void error(Throwable e) {
		logger.error(e);
	}

	
	private static String _name;
	
	public static final String getName(){
		return _name;
	}
	
	public static void setName(String name) {
		_name = name;
		logger = LogFactory.getLog(name);
	}
	public static void error(String string, Throwable e) {
		logger.error(string,e);
	}
	public static void debug(boolean b) {
		debug = b;
	}

	public static void info(Object value) {
		final boolean d = debug;
		if (d) {
			logger.info(value);
		}
	}
	
	public static void info(Object value, boolean input) {
		if (input) {
			logger.info(value);
		}else {
			info(value);
		}
	}
	
	
	public IActionFactory getDefaultActionFactory() {
		return ApiJsonActionFactory.getDefault();
	}
	

	@Override
	public void registerActions(Actions actions) {
		actions.add(new TokenAction("/gate/token"));
		actions.add(new TokenAction("/gate/requestToken"));
		actions.add(new ServerNotifyAction("/serverNotify"));
		actions.add(new ApiInterfaces("/interface"));
		actions.add(new PayServerNotifyAction("/payNotify"));
		actions.add(new ProxyParamAction("/proxy0"));
		actions.add(new ProxyAction("/proxy1"));
		actions.add(new ProxyAction("/proxy2"));
		actions.add(new ProxyAction("/proxy3"));
		
		
		
		actions.add(ProxyApi.class);
		actions.add(JobCenterApi.class);
		actions.add(StructsWebController.class);
		actions.add(StructsApi.class);
		actions.add(GenerateService.class);
		
		actions.add(  PhpVersionController.class  );
		
	}

	@Override
	public void registerPlugins(List<IPlugin> plugins) {
		ConfigReader reader = ConfigReader.getDefault();
		HttpUtil.READ_TIMEOUT = reader.getInt("CONNECTION.READ_TIMEOUT");
		HttpUtil.CONNECT_TIMEOUT = reader.getInt("CONNECTION.CONNECT_TIMEOUT");

		httpClientPlugin = new ApacheHttpPlugin();
		plugins.add(httpClientPlugin);

		DbPlugin druidPlugin = new DbPlugin();
		List<ConnectionDescription> list;
		try {
			list = DbConfigUtil.loadConfig();
			for (ConnectionDescription connectionDescription : list) {
				druidPlugin.add(connectionDescription);
			}
		} catch (IOException e) {
			//throw new RuntimeException("加载数据配置出错", e);
		}

		plugins.add(druidPlugin);
		
		try {
			Class.forName("net.rubyeye.xmemcached.MemcachedClient");
			String memcache = ConfigReader.getDefault().get("memcache");
			if(!"false".equals(memcache)){
				plugins.add(new MemcachePlugin());
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		
		
		TaskQueue.getInstance().setThreadCount(10);
		plugins.add(TaskQueue.getInstance());
		try {
			Class.forName("org.quartz.CronScheduleBuilder");
			plugins.add(QuartzPlugin.me());
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	/**
	 * 
	 */
	@Override
	public void beforeStart() {
	//	SyncLockFactory.init(100);
		/**
		 * 保存数据库快照
		 */
		TaskQueue.getInstance().registerAsyncJobFactory(AopAsyncJobFactory.NAME, new AopAsyncJobFactory() );
	}

	@Override
	public void shutDown() {

	}
	
	@Override
	public void registerService(List<IService> services) {
	
	}



	public static void warn(String string) {
		logger.warn(string);
	}



	public void registerEntities(List<Class<?>> entities) {
		// TODO Auto-generated method stub
		
	}

	public IDataSourceProvider createDataSourceProvider(ConnectionDescription connectionDescription) {
		return new DruidDataSourceProvider(connectionDescription);
	}
	

	



	




}
